﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using IFPRJ.EF.DALayer;
using IFPRJ.EF.DALayer.Providers.Sqlserver;
using IFPRJ.EF.Model;
using IFPRJ.EF.Components;
using System.Web;

namespace IFPRJ.Areas.Admin
{
    public class NewsController:AdminBaseController
    {
        INews newsprovider = new NewsProvider();
        INewsCatory provider = new NewsCatoryProvider();
        private SelectList GetDropDownList(int? id)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            var news = provider.NewsCatoryLoadAll();
            return  new SelectList(news, "Id", "Name",id.ToString());
        }
        public ActionResult Index()
        {
            var newslist = newsprovider.NewsLoadAll();
            this.ViewData["newslist"] = newslist;
            return View("Index",new List<News>(newslist));
        }

        public ActionResult Create()
        {
            this.ViewData["newscatorylist"] = GetDropDownList(null);
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(News _news, string newscatorylist, string FckTextBox)
        {
            int catoryid = Convert.ToInt32(newscatorylist);
            _news.Context = FckTextBox;
            _news.Date_Post = DateTime.Now;
            _news.News_CatoryID = catoryid;
            newsprovider.NewsCreate(_news);
            return RedirectToAction("Index");
        }


        public ActionResult Edit(int id)
        {
            News news = newsprovider.NewsGetSingleByNewsId(id);
            this.ViewData["newscatorylist"] = GetDropDownList(news.News_CatoryID);
            return View(news);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [HttpPost, ActionName("Edit")]
        public ActionResult EditSave(int id,News _news, string newscatorylist, string FckTextBox)
        {
            _news.ID = id;
            _news.Context = FckTextBox;
            _news.Date_Lastupdated = DateTime.Now;
            _news.News_CatoryID = Convert.ToInt32(newscatorylist);
            if (ModelState.IsValid)
            {
                newsprovider.NewsUpdate(_news);
                return RedirectToAction("Index");
            }
            return RedirectToAction("Edit");
        }



        public ActionResult Delete(int id)
        {
            News deletenews = newsprovider.NewsGetSingleByNewsId(id);
            return View(deletenews);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            News deletenews = newsprovider.NewsGetSingleByNewsId(id);
            newsprovider.NewsDelete(deletenews);

            return RedirectToAction("Index");
        }

    }
}
